package org.apache.rocketmq.proxy;

import com.alibaba.fastjson.JSON;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.apache.rocketmq.remoting.netty.NettyDecoder;
import org.apache.rocketmq.remoting.netty.NettyEncoder;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
import org.apache.rocketmq.remoting.protocol.RequestCode;
import org.apache.rocketmq.remoting.protocol.header.CreateTopicRequestHeader;
import org.apache.rocketmq.remoting.protocol.header.namesrv.GetRouteInfoRequestHeader;


/**
 * @课程描述:从零带你写框架系列中的课程，整个系列包含netty，xxl-job，rocketmq，nacos，sofajraft，spring，springboot，disruptor，编译器，虚拟机等等。
 * @author：陈清风扬，个人微信号：chenqingfengyangjj。
 * @date:2024/12/25
 * @方法描述：客户端测试类
 */
public class NettyCLient {

    public static void main(String[] args) throws InterruptedException {

        NioEventLoopGroup workerGroup = new NioEventLoopGroup(1);

        try {
            Bootstrap b = new Bootstrap();
            b.group(workerGroup)
                    .channel(NioSocketChannel.class)
                    //这里设置的是Proxy服务器的地址和端口
                    .remoteAddress("localhost", 28080)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new NettyEncoder(), new NettyDecoder())
                                            .addLast(new ChannelDuplexHandler() {
                                                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                                    RemotingCommand remotingCommand = (RemotingCommand) msg;
                                                    System.out.println("receive response: " + JSON.toJSONString(remotingCommand));
                                                    ctx.fireChannelRead(remotingCommand);
                                                }
                                            });
                        }
                    })
                    .option(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture f = b.connect().sync();


            //创建请求头
//            CreateTopicRequestHeader requestHeader = new CreateTopicRequestHeader();
//            //把主题信息设置到请求头中，要获得的就是该主题的路由信息
//            requestHeader.setTopic("TestTopic");
//            requestHeader.setDefaultTopic("TBW102");
//            requestHeader.setReadQueueNums(4);
//            requestHeader.setWriteQueueNums(4);
//            requestHeader.setPerm(6);
//            requestHeader.setTopicFilterType("SINGLE_TAG");
//            requestHeader.setOrder(false);
//            requestHeader.setTopicSysFlag(0);
            GetRouteInfoRequestHeader requestHeader = new GetRouteInfoRequestHeader();
            requestHeader.setTopic("TestTopic");


            //把请求头封装到Remoting协议的对象中
//            RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_AND_CREATE_TOPIC, requestHeader);
            RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.GET_ROUTEINFO_BY_TOPIC, requestHeader);
            //在这里把请求发送给Proxy服务器
            f.channel().writeAndFlush(request);


            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
        }
    }
}
